// pages/login/login.js
import {
  httpRequest
} from "../../utils/request/http"

Page({

  /**
   * 页面的初始数据
   */
  data: {
    avatarUrl: "",
    userInfo: {}
  },
  onChooseAvatar(e) {
    this.setData({
      avatarUrl: e.detail.avatarUrl
    })
  },
  submit(e) {

    wx.login({
      success: (res) => {
        if (res.code) {
          let query = {
            nickName: e.detail.value.nickName,
            avatarUrl: this.data.avatarUrl,
            code: res.code
          }
          httpRequest("/api/wx_login", "post", query).then((res) => {
            if (res.is_vip === 0) {
              res.is_vip = "普通用户"
            } else {
              res.is_vip = "会员用户"
            }
            if (res.gender === 1) {
              res.gender = "靓仔"
            } else {
              res.gender = "靓女"
            }
            let auth_token = res.token

            wx.setStorageSync('auth_token', auth_token)
            let user_info = {
              avatarUrl: res.avatarUrl,
              create_at: res.create_at,
              expireAt: res.expireAt,
              gender: res.gender,
              is_vip: res.is_vip,
              nick_name: res.nick_name,
              refreshAfter: res.refreshAfter,
              total_available: res.total_available,
              total_expire_at: res.total_expire_at
            }
            this.setData({
              userInfo: user_info
            })
            wx.setStorageSync('userInfo', user_info)
            wx.showToast({
              title: '登录成功',
            })
            setTimeout(() => {
              wx.switchTab({
                url: '/pages/home/home',
              })
            }, 1500);
          })
        }
      },
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})